import React, { Component } from 'react';
import { Checkbox } from 'antd';
import Label from '../LabelDecorator';

@Label()
class FormCheckbox extends Component {
  renderStaticOptions = style => {
    const options = this.props.getData('datasource').value;
    if (!Array.isArray(options)) {
      return null;
    }
    return options.map(option => (
      <Checkbox style={style} value={option} key={option}>
        {option}
      </Checkbox>
    ));
  };

  renderDynamicOptions = style => {
    const options = [];
    return options.map(option => (
      <Checkbox style={style} value={option} key={option}>
        {option}
      </Checkbox>
    ));
  };

  render() {
    const { getData } = this.props;
    const isStatic = getData('datasource').type === 'static';
    const defaultValue = getData('defaultValue');
    const direction = getData('direction');
    const radioStyle =
      direction === 'vertical'
        ? {
            display: 'block',
            height: '30px',
            lineHeight: '30px',
            marginLeft: 0
          }
        : undefined;
    return (
      <Checkbox.Group defaultValue={defaultValue}>
        {isStatic ? this.renderStaticOptions(radioStyle) : null}
      </Checkbox.Group>
    );
  }
}

export default FormCheckbox;
